package superAdmin;

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;
/*
    用户的增删改查（学院、班级的增删）
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AddUser extends addClass{
    @Override
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "../superadminLogin.csv")
    void login(String name, String password, String loginURL, String toURL) throws InterruptedException {
        super.login(name, password, loginURL, toURL);
    }

    @Override
    @Order(2)
    @ParameterizedTest
    @CsvSource("计科,1001")
    void addCollege(String collegeName, String collegeId) {
        super.addCollege(collegeName, collegeId);
    }

    @Override
    @Order(3)
    @ParameterizedTest
    @CsvSource("计科,1班")
    void addClass(String collegeName, String className) throws InterruptedException {
        super.addClass(collegeName, className);
    }

    @ParameterizedTest
    @CsvFileSource(resources = "../adduser.csv")
    @Order(4)
    void addUser(String username,String name) throws InterruptedException {
        //选中人员管理
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.left-menu > ul > li:nth-child(2) > ul > li:nth-child(2) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //点击 添加人员
        webDriver.findElement(By.cssSelector("#admin > div.main > div.wa.container.clearfix > div.right-content > div > div.manage-person-head > div > div > a:nth-child(1) > button"))
                .click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.right-content > div > form > div:nth-child(2) > div > div > div > input"))
                .sendKeys(username);
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.right-content > div > form > div:nth-child(3) > div > div > div > input"))
                .sendKeys(name);
        //选择教师
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.right-content > div > form > div:nth-child(4) > div > div > div > label.el-radio.is-checked"))
                .click();
        //选择学院
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.right-content > div > form > div:nth-child(5) > div > div > div > div > input")).click();
        sleep(1000);
        webDriver.findElement(By.cssSelector("body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li:nth-child(3)")).click();
        sleep(3000);
        //班级
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.right-content > div > form > div:nth-child(6) > div > div > div > div.el-input.el-input--suffix > input"))
                        .click();
        sleep(1000);
        webDriver.findElement(By.cssSelector("body > div.el-select-dropdown.el-popper.is-multiple > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li")).click();
        sleep(3000);
        //确定
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.right-content > div > form > div:nth-child(13) > div > div > button.el-button.el-button--primary.el-button--mini"))
                .click();
        sleep(1000);
    }

    @ParameterizedTest
    @CsvFileSource(resources = "../adduser.csv")
    @Order(5)
    void searchAndDel(String username) throws InterruptedException {
        //输入用户名
        webDriver.findElement(By.cssSelector("#admin > div.main > div.wa.container.clearfix > div.right-content > div > div.manage-person-head > form > div:nth-child(2) > div:nth-child(1) > div > div > input"))
                .sendKeys(username);
        //查询
        webDriver.findElement(By.cssSelector("#admin > div.main > div.wa.container.clearfix > div.right-content > div > div.manage-person-head > form > div:nth-child(2) > button:nth-child(3)"))
                .click();
        sleep(3000);
        //选中
        webDriver.findElement(By.cssSelector("#admin > div.main > div.wa.container.clearfix > div.right-content > div > div.manage-person-main > div > div > div.el-table__header-wrapper > table > thead > tr > th.el-table_1_column_1.is-center.el-table-column--selection.is-leaf > div > label"))
                .click();
        //删除
        webDriver.findElement(By.cssSelector("#admin > div.main > div.wa.container.clearfix > div.right-content > div > div.manage-person-head > div > div > a:nth-child(4) > button")).click();
        sleep(1000);
        webDriver.findElement(By.cssSelector("body > div.el-message-box__wrapper > div > div.el-message-box__btns > button.el-button.el-button--default.el-button--small.el-button--primary.confirm-btn")).click();
        sleep(3000);
    }



    @Override
    @Order(6)
    @Test
    void deleteClass() throws InterruptedException {
      webDriver.findElement(By.xpath("//*[@id=\"admin\"]/div[2]/div/div[1]/ul/li[2]/ul/li[1]/a")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        List<WebElement> colleges = webDriver.findElements(By.cssSelector("h1 > font"));
        //选中新添加的学院
        System.out.println(colleges.size());
        for (int i = 0; i < colleges.size(); i++) {
            if (colleges.get(i).getText().equals("计科")){
                colleges.get(i).click();
            }
        }
        //选择刚才新添加的班级
        sleep(3000);
        webDriver.findElement(By.cssSelector("#schoolBox > div.class-list > ul > li")).click();
        //点击删除
        webDriver.findElement(By.cssSelector("#schoolBox > div.classInfo > form > div.btnList > button.el-button.btn.deleteBtn.el-button--default.el-button--small > span"))
                .click();
        //点击确定
        webDriver.findElement(By.cssSelector("body > div.el-message-box__wrapper > div > div.el-message-box__btns > button.el-button.el-button--default.el-button--small.el-button--primary.confirm-btn > span"))
                .click();
        sleep(3000);
    }

    @Override
    @Order(6)
    @ParameterizedTest
    @CsvSource("计科")
    void deleteCollege(String collegeName) throws InterruptedException {
        super.deleteCollege(collegeName);
    }

}
